package indi.lqzhi.tcpserver.manager;

import io.netty.channel.Channel;

import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: LQZhi
 * @ClassName: BusManager
 * @Date: 2021-09-30 07-26
 * @Description: This is a BusManager Class.
 * @Version: V1.0
 */
public class BusManager extends AbstractManager<String, Channel> {

    /**
     * 注册监听器
     */
    private final Collection<OnRegisterChannelListener> onRegisterChannelListeners;

    /**
     * 移除监听器
     */
    private final Collection<OnUnRegisterChannelListener> onUnRegisterChannelListeners;

    public BusManager(){
        //初始化
        super();
        onRegisterChannelListeners = new CopyOnWriteArrayList<>();
        onUnRegisterChannelListeners = new CopyOnWriteArrayList<>();
    }

    /**
     * 重置管理器，清空所有
     */
    @Override
    public void resetManager() {
        super.resetManager();
        onRegisterChannelListeners.clear();
        onUnRegisterChannelListeners.clear();
    }

    /**
     * 注册注册监听器
     * @param listener 监听器
     * @return true 注册成功 false 注册失败
     */
    public boolean addOnRegisterChannelListener(OnRegisterChannelListener listener){
        return listener != null && onRegisterChannelListeners.add(listener);
    }

    /**
     * 移除注册监听器
     * @param listener 监听器
     * @return true 移除成功 false 移除失败
     */
    public boolean removeOnRegisterChannelListener(OnRegisterChannelListener listener){
        return listener != null && onRegisterChannelListeners.remove(listener);
    }

    /**
     * 注册移除监听器
     * @param listener 监听器
     * @return true 注册成功 false 注册失败
     */
    public boolean addOnUnRegisterChannelListener(OnUnRegisterChannelListener listener){
        return listener != null && onUnRegisterChannelListeners.add(listener);
    }

    /**
     * 移除移除监听器
     * @param listener 监听器
     * @return true 移除成功 false 移除失败
     */
    public boolean removeOnUnRegisterChannelListener(OnUnRegisterChannelListener listener){
        return listener != null && onUnRegisterChannelListeners.remove(listener);
    }

    /**
     * 注册监听回调器
     */
    @FunctionalInterface
    public  interface OnRegisterChannelListener{
        /**
         * 处理器
         * @param manager 管理者
         * @param key 注册的key
         * @param value 注册的value
         */
        void onHandler(Manager<String,Channel> manager,String key,Channel value);
    }

    /**
     * 异常监听回调器
     */
    @FunctionalInterface
    public interface OnUnRegisterChannelListener {

        /**
         * 处理器
         * @param manager 管理者
         * @param key 移除的key
         * @param value 移除的value
         */
        void onHandler(Manager<String,Channel> manager,String key,Channel value);
    }

}
